﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using PMPUtils.Goals;
using PMPUtils.Form;
using System.Drawing;

namespace PMP.Goals
{
    public partial class ViewGoalDetails : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            FormUtil.Authenticate(Session, Response);

            if (!IsPostBack)
            {
                GoalDetailsOperator.InitializeGoalDetailsGridView(grvGoalDetails);
                txtGoal.Text = Request.Params["PARENT_ID"];
            }

            RefreshForm();
        }

        protected void grvGoalDetails_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            // Convert the row index stored in the CommandArgument
            // property to an Integer.
            int index = Convert.ToInt32(e.CommandArgument);

            // Retrieve the row that contains the button clicked 
            // by the user from the Rows collection.
            GridViewRow row = grvGoalDetails.Rows[index];

            String goalDetailID = GridViewOperator.GetCellStringValue(grvGoalDetails, row, "ID");
            String taskID = GridViewOperator.GetCellStringValue(grvGoalDetails, row, "Task");

            if (e.CommandName.Equals("ManageGoalDetails"))
            {
                PopupForm manageGoalDetails = new PopupForm(Response, "GoalDetailManagement.aspx?GOALDETAIL=" + goalDetailID + "&TASK=" + taskID, "GoalDetailManagement", "225", "300", "350", "200");
                manageGoalDetails.Popup();
            }
        }

        protected void grvGoalDetails_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                // Tool tip for task
                int cellID = GridViewOperator.GetCellID(grvGoalDetails, "Task");
                String taskID = e.Row.Cells[cellID].Text;

                if (taskID != null && !taskID.Equals(String.Empty) && !taskID.Equals("&nbsp;"))
                {
                    HyperLink task = new HyperLink();
                    task.Text = e.Row.Cells[cellID].Text;
                    task.NavigateUrl = "http://jira.redprairie.com/browse/" + e.Row.Cells[cellID].Text;
                    e.Row.Cells[cellID].Controls.Add(task);
                }
            }
        }

        protected void btnAddGoalDetail_Click(object sender, EventArgs e)
        {
            String ID = txtGoal.Text.Trim();
            PopupForm addGoalDetails = new PopupForm(Response, "AddGoalDetail.aspx?PARENT_ID=" + ID, "AddGoalDetail", "600", "500", "300", "300");
            addGoalDetails.Popup();
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            try
            {
                RefreshForm();
            }
            catch (Exception ex)
            {
                Messenger.Show(Response, "Error: " + ex.Message);
            }

            Messenger.Show(Response, "Refresh Successfully.");
        }

        private void RefreshForm()
        {
            try
            {
                String parentGoalID = txtGoal.Text.Trim();
                GoalDetailsOperator.RefreshGoalDetailsGridView(grvGoalDetails, parentGoalID);
            }
            catch (Exception ex)
            {
                Messenger.Show(Response, "Error:" + ex.Message);
                return;
            }

            GridViewOperator.RefreshJIRAStatusBackgroundColor(grvGoalDetails);

            FormatGridView();
        }

        private void FormatGridView()
        {
            // Status
            foreach (GridViewRow row in grvGoalDetails.Rows)
            {
                // Tool tip for task
                int jiraNumberCellID = GridViewOperator.GetCellID(grvGoalDetails, "Task");
                String jiraNumber = GridViewOperator.GetCellStringValue(grvGoalDetails, row, "Task");
                String goalID = GridViewOperator.GetCellStringValue(grvGoalDetails, row, "ID");

                String description = GoalDetailsOperator.ReadTips(goalID);
                row.Cells[jiraNumberCellID].ToolTip = description;

                // Variance
                int varianceCellID = GridViewOperator.GetCellID(grvGoalDetails, "Variance");

                if (row.Cells[varianceCellID] == null || row.Cells[varianceCellID].Text.Equals(String.Empty) || row.Cells[varianceCellID].Text.Equals("&nbsp;"))
                {
                    continue;
                }

                double variance = Convert.ToDouble(row.Cells[varianceCellID].Text.Substring(0, row.Cells[varianceCellID].Text.Length - 1));

                if (variance >= 25)
                {
                    row.Cells[varianceCellID].BackColor = Color.Red;
                }
                else if (variance >= 0)
                {
                    row.Cells[varianceCellID].BackColor = Color.Yellow;
                }
            }
        }
    }
}
